package com.paic.mhis.hcpms.agreement.healthcareprovider.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.MedicalInfoservice;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.ValidatorUtil;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.web.BaseRest;

@Controller
public class MedicalInfoController extends BaseRest {
	@BizServicesProxy
	private MedicalInfoservice medicalInfoservice;

	public void setMedicalInfoservice(MedicalInfoservice medicalInfoservice) {
		this.medicalInfoservice = medicalInfoservice;
	}

	/**
	 * 医疗设备列表查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/medical/page")
	public Map<String, Object> queryMedicalInfoList(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String providerid = request.getParameter("providerid");
			String faclityName = request.getParameter("faclityName");
			String faclityCode = request.getParameter("faclityCode");
			String page = request.getParameter("page");
			String pageSize = request.getParameter("pageSize");
			if (!isEmpty(page) || !isEmpty(pageSize)) {
				if (!ValidatorUtil.isPlusInteger(page)
						|| !ValidatorUtil.isPlusInteger(pageSize)) {
					result = new HashMap<String, Object>();
					HttpProcessUtil.generateExceptionResult(result,
							Constants.STATE_KEY,
							Constants.FAILED_VALIDATE_STATE_VALUE,
							Constants.MSG_KEY,
							Constants.PAGE_VALIDATE_FAILED_MSG);
					return result;
				}
			}
			JSONObject transferJson = new JSONObject();
			transferJson.put("page", page);
			transferJson.put("pageSize", pageSize);
			transferJson.put("providerid", providerid);
			transferJson.put("faclityName", faclityName);
			transferJson.put("faclityCode", faclityCode);
			result = medicalInfoservice.queryMedicalInfoList(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医疗设备详情查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/medical/show")
	public Map<String, Object> queryMedicalDetail(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String id = request.getParameter("id");
			JSONObject transferJson = new JSONObject();
			transferJson.put("id", id);
			result = medicalInfoservice.queryMedicalDetail(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医疗设备新增
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/medical/create", method = RequestMethod.POST)
	public Map<String, Object> addMedicalFacility(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJson(request, transferJson);
			result = medicalInfoservice.addMedicalFacility(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医疗设备信息修改
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/medical/update", method = RequestMethod.POST)
	public Map<String, Object> updateMedicalFacility(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJsonUpdate(request, transferJson);
			result = medicalInfoservice.updateMedicalFacility(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 违规信息批量删除
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/medical/delete", method = RequestMethod.POST)
	public Map<String, Object> deleteMedicalFacility(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJsonUpdate(request, transferJson);
			result = this.medicalInfoservice
					.deleteMedicalFacility(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	private boolean isEmpty(String param) {
		return param == null || param.equals("");
	}
}
